package com.wl.dzt.sys.controller;

import com.wl.dzt.sys.core.dao.template.OrmTemplate;
import com.wl.dzt.sys.core.dao.template.bean.ManyToMany;
import com.wl.dzt.sys.core.dao.template.bean.TagerInfo;
import com.wl.dzt.sys.util.WebUtils;
import com.wl.dzt.sys.util.json.Json;
import com.google.common.reflect.TypeToken;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/7/5/005
 * @description orm控制器
 */
@RestController
@RequestMapping("admin/orm")
@ApiIgnore
public class OrmController {

    @Resource
    private OrmTemplate ormTemplate;

    /**
     * <b>多对多保存</b>

     */
    @RequestMapping("manyToMany")
    public void manyToMany(HttpServletRequest request, HttpServletResponse response, ManyToMany many, String infos) {
        List<TagerInfo> tagerInfos = Json.recover(infos, new TypeToken<List<TagerInfo>>() {
        }.getType());
        many.setTagerInfos(tagerInfos);
        ormTemplate.manyToMany(many);
    }

    /**
     * <b>多对多加载</b>

     */
    @RequestMapping("getManyToMany")
    public void getManyToMany(HttpServletRequest request, HttpServletResponse response, String json) {
        Map<String, String> param = Json.recover(json, new TypeToken<Map<String, String>>() {
        }.getType());
        WebUtils.json(ormTemplate.getManyToMany(param), response);
    }
}
